//
//  HomeAddModuleViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/4.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "HomeAddModuleModel.h"
#import "AddModuleManager.h"
#import "HomeOrganizationalViewController.h"
#import "HomeAddModuleViewController.h"
#import "HomeAddModuleSectionHeaderView.h"
#import "HomeAddModuleCell.h"
#import "HomeDeleteModuleCell.h"
@interface HomeAddModuleViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,HomeAddModuleCellDelegate>
/** View */
@property (weak, nonatomic) UICollectionView * collectionView;
/** 已添加 */
@property (nonatomic,strong) NSMutableArray<HomeAddModuleHomeconfigdomain  *>  * homeConfigDomain;
/** 未添加 */
@property (nonatomic,strong) NSMutableArray<HomeAddModuleHomeconfigdomain  *>  * nohomeConfigDomain ;

/** homeAddModuleModel */
@property (nonatomic,strong) HomeAddModuleModel * homeAddModuleModel;
@end

@implementation HomeAddModuleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexRGB(0xf5f5f5);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"添加模块";
    // Do any additional setup after loading the view.
    [self addCollectionView];
    
    [self addRequest];
}
- (void)addRequest
{
    [[ZXHTTPManager sharedManager] getHomeConfAllWithCompleted:^(NSDictionary *jsonDict, NSError *error) {
       
        logError
        
        NSLog(@"jsonDict = %@",jsonDict);
        HomeAddModuleModel * homeAddModuleModel = [HomeAddModuleModel mj_objectWithKeyValues:jsonDict];
        self.homeAddModuleModel = homeAddModuleModel;
        
        self.homeConfigDomain = homeAddModuleModel.result.homeConfigDomain.mutableCopy;
        self.nohomeConfigDomain = homeAddModuleModel.result.nohomeConfigDomain.mutableCopy;
        [AddModuleManager sharedManager].homeAddModuleHomeconfigdomains = homeAddModuleModel.result.homeConfigDomain.mutableCopy;
        
        [self.collectionView reloadData];
    }];
}
- (void)addCollectionView
{
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    /** 垂直方向 间距 */
    flowLayout.minimumLineSpacing = 7.5;
    /** 横向方向 间距 */
    flowLayout.minimumInteritemSpacing = 7.5;
    /** 内缩 内容 */
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    NSInteger colNum = 3;
    
    CGFloat w =  ([UIScreen mainScreen].bounds.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing * (colNum - 1)) / colNum;
    
    /** item大小 */
    flowLayout.itemSize = CGSizeMake(w,110);
    /** headerView大小 */
    flowLayout.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 36);
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) collectionViewLayout:flowLayout];
    
    self.collectionView = collectionView;
    /** 分页处理 */
    // collectionView.pagingEnabled = YES;
    
    collectionView.backgroundColor = HexRGB(0xF5F5F5);;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerClass:[HomeAddModuleCell class] forCellWithReuseIdentifier:@"HomeAddModuleCell"];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[HomeDeleteModuleCell class] forCellWithReuseIdentifier:@"HomeDeleteModuleCell"];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[HomeAddModuleSectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeAddModuleSectionHeaderView"];
}
#pragma mark - <UICollectionViewDataSource>
/** section 数量 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
/** item 数量 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    switch (section) {
        case 0:
//            count = self.addArrM.count;
            count = self.homeConfigDomain.count;
            break;
        case 1:
//            count = self.deleteArrM.count;
            count = self.nohomeConfigDomain.count;
            break;
        default:
            break;
    }
    return count;
}
- (void)deleteItem:(NSIndexPath *)indexPath
{
    HomeAddModuleCell * cell = (HomeAddModuleCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    
    HomeAddModuleHomeconfigdomain * domain = cell.homeAddModuleHomeconfigdomain;
    
    [self.homeConfigDomain removeObject:domain];
    [self.nohomeConfigDomain addObject:domain];
    
    NSMutableArray * arrM = [AddModuleManager sharedManager].homeAddModuleHomeconfigdomains.mutableCopy;
    
    for (HomeAddModuleHomeconfigdomain * obj in arrM.reverseObjectEnumerator) {
        
        if([obj.resourceName isEqualToString:domain.resourceName]){
            [arrM removeObject:obj];
        }
        
    }
    [AddModuleManager sharedManager].homeAddModuleHomeconfigdomains = arrM;
    
    NSLog(@"[AddModuleManager sharedManager].homeAddModuleHomeconfigdomains = %@",[AddModuleManager sharedManager].homeAddModuleHomeconfigdomains);
    [self.collectionView reloadData];
}
/** UICollectionViewCell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * collectionViewCell;
    switch (indexPath.section) {
        case 0:
        {
            HomeAddModuleCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeAddModuleCell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            
//            [cell config:self.addArrM[indexPath.item] indexPath:indexPath];
            [cell configDomain:self.homeConfigDomain[indexPath.item] indexPath:indexPath];
            
            
            cell.delegate = self;
            
            collectionViewCell = cell;
            
        }
            break;
        case 1:
        {
            HomeDeleteModuleCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeDeleteModuleCell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
//            cell.model = self.deleteArrM[indexPath.item];
            cell.homeAddModuleHomeconfigdomain = self.nohomeConfigDomain[indexPath.item];
            collectionViewCell = cell;
        }
            break;
        default:
            break;
    }
    return collectionViewCell;
}
/** UICollectionReusableView */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        //此处是headerView
        HomeAddModuleSectionHeaderView *  headerReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HomeAddModuleSectionHeaderView" forIndexPath:indexPath];
        // headerReusableView.backgroundColor = [UIColor yellowColor];
        switch (indexPath.section) {
            case 0:
            {
                headerReusableView.titleLabel.text = @"已添加";
            }
                break;
            case 1:
            {
                headerReusableView.titleLabel.text = @"未添加";
            }
                break;
            default:
                break;
        }
        
        
        return headerReusableView;
    }else if(kind == UICollectionElementKindSectionFooter){
        //此处是footerView
        UICollectionReusableView *  footerReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableViewFooter" forIndexPath:indexPath];
        footerReusableView.backgroundColor = [UIColor greenColor];
        footerReusableView.layer.borderColor = [UIColor blackColor].CGColor;
        footerReusableView.layer.borderWidth = 1.0;
        return footerReusableView;
    }else{
        return nil;
    }
}
#pragma mark - <UICollectionViewDelegate>
// 选中操作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
    
    switch (indexPath.section) {
        case 0: //添加
        {
//            HomeAddModuleCell * cell = (HomeAddModuleCell *)[collectionView cellForItemAtIndexPath:indexPath];
//            DBModel * model = cell.model;
//            model.addition = NO;
//            [self.deleteArrM addObject:model];
//            [self.addArrM removeObject:model];
//            
//            self.models = self.addArrM;
//            [self.collectionView reloadData];
            
        }
            break;
        case 1: //删除
        {
            HomeDeleteModuleCell * cell = (HomeDeleteModuleCell *)[collectionView cellForItemAtIndexPath:indexPath];

            HomeAddModuleHomeconfigdomain * domain = cell.homeAddModuleHomeconfigdomain;
            
            [self.homeConfigDomain addObject:domain];
            [self.nohomeConfigDomain removeObject:domain];
            
//            [[AddModuleManager sharedManager].resourceNames addObject:domain.resourceName];
            [[AddModuleManager sharedManager].homeAddModuleHomeconfigdomains addObject:domain];
            
            NSLog(@"[AddModuleManager sharedManager].homeAddModuleHomeconfigdomains = %@",[AddModuleManager sharedManager].homeAddModuleHomeconfigdomains);
            [self.collectionView reloadData];
        }
            break;
        default:
            break;
    }
}
-(void)dealloc{
    
    NSArray * ids =  [self.homeConfigDomain valueForKey:@"ID"];
    NSLog(@"%@",ids);
    
    [[ZXHTTPManager sharedManager] updateHomeConfWithResourceIdList:ids completed:^(NSDictionary *jsonDict, NSError *error) {
       
        logError;
        
        NSLog(@"%@",jsonDict);
    }];
}
// 取消选中操作
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
}
//-(void)dealloc{
//    NSLog(@"self.addArrM = %@",self.addArrM);
//    NSLog(@"self.models = %p",self.models);
//    self.models = self.addArrM;
//    [HomeOrganizationalViewController setModels:self.addArrM];
//    NSLog(@"[AddModuleManager sharedManager].resourceNames = %@",[AddModuleManager sharedManager].resourceNames);
//}
//-(void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:animated];
//    
//    NSLog(@"self.addArrM = %@",self.addArrM);
//    NSLog(@"self.models = %p",self.models);
////    self.models = self.addArrM;
//    [HomeOrganizationalViewController setModels:self.addArrM];
//}
@end
